package com.dennytech.tac.widget;

import java.io.ByteArrayOutputStream;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;

import com.dennytech.tac.R;
import com.dennytech.tac.utils.Log;
import com.dennytech.tac.utils.db.ChannelDBHelper;

public class ChannelImageView extends NetworkThumbView {

	private static final String LOG_TAG = ChannelImageView.class
			.getSimpleName();

	private Context mContext;

	public ChannelImageView(Context context) {
		super(context);
		mContext = context;
	}

	public ChannelImageView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
	}

	public ChannelImageView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		mContext = context;
	}

	@Override
	public void setImage(String url) {
		int resId = 0;
		// add native image here
		if (url.endsWith("ic_my_collect.png")) {
			resId = R.drawable.ic_my_collect;
		} else if (url.endsWith("ic_travel_note.png")) {
			resId = R.drawable.ic_travel_note;
		} else if (url.endsWith("icon_add.png")) {
			resId = R.drawable.icon_add;
		}

		if (resId != 0) {
			Resources r = mContext.getResources();
			Drawable drawable = r.getDrawable(resId);

			this.url = url;
			imageRetrieve = true;
			setImageDrawable(drawable);
		} else {
			super.setImage(url);
		}
	}

	@Override
	protected LoadTask createLoadTask() {
		return new ChannelPhotoLoadTask(this);
	}

	protected static class ChannelPhotoLoadTask extends CachedLoadTask {
		ChannelDBHelper dbHelper;

		public ChannelPhotoLoadTask(NetworkImageView p) {
			super(p);
			dbHelper = ChannelDBHelper.instance();
		}

		@Override
		public Bitmap preDoInBackground(String... params) {
			final String url = params[0];
			int i = url.lastIndexOf("/");
			if (i > 0) {
				urlLocks.lock(url);
				try {
					//
					// check memcache first
					//
					Bitmap cbitmap = memcache.get(url);
					if (cbitmap != null)
						return cbitmap;

					Bitmap bitmap = null;

					byte[] photoByte = dbHelper.queryChannelPhoto(url);
					if (photoByte != null) {
						bitmap = BitmapFactory.decodeByteArray(photoByte, 0,
								photoByte.length);
						Log.i(LOG_TAG, "load bitmap from db success:" + url);
					} else {
						Log.i(LOG_TAG, "load bitmap from db failed:" + url);
					}

					if (bitmap != null)
						memcache.put(url, bitmap);
					return bitmap;
				} catch (Exception e) {
				} finally {
					urlLocks.unlock(url);
				}
			}

			return null;
		}

		@Override
		public Bitmap doInBackground(String... params) {
			Bitmap bm = super.doInBackground(params);
			if (bm != null) {
				ByteArrayOutputStream baos = new ByteArrayOutputStream();
				bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
				byte[] photoByte = baos.toByteArray();
				dbHelper.updateChannelPhoto(params[0], photoByte);
			}
			return bm;
		}

	}

}
